using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserBll
/// </summary>
public class UserBll
{
    DBAccess db = new DBAccess();

    public UserBll()
    {

    }
        
    public DataTable getAllUser()
    {
        return db.executeSelect("GetAllUser");
    }

    public User getUserByUsername(string username)
    {
        User user = new User();
        DataTable dt = new DataTable();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@Username", SqlDbType.NVarChar);
        para[0].Value = username;
        dt = db.executeSelect("GetUserByUsername", para);
        user.UserId = int.Parse(dt.Rows[0][0].ToString());
        user.UserName = dt.Rows[0][1].ToString();
        user.Password = dt.Rows[0][2].ToString();
        user.FullName = dt.Rows[0][3].ToString();
        user.Gender = dt.Rows[0][4].ToString();
        user.Address = dt.Rows[0][5].ToString();
        user.Role = dt.Rows[0][6].ToString();
        user.Phone = dt.Rows[0][7].ToString();
        user.Email = dt.Rows[0][8].ToString();
        return user;
    }

    public User getUserById(int userId)
    {
        User user = new User();
        DataTable dt = new DataTable();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@UserId", SqlDbType.Int);
        para[0].Value = userId;
        dt = db.executeSelect("GetUserByUsername", para);
        user.UserId = int.Parse(dt.Rows[0][0].ToString());
        user.UserName = dt.Rows[0][1].ToString();
        user.Password = dt.Rows[0][2].ToString();
        user.FullName = dt.Rows[0][3].ToString();
        user.Gender = dt.Rows[0][4].ToString();
        user.Address = dt.Rows[0][5].ToString();
        user.Role = dt.Rows[0][6].ToString();
        user.Phone = dt.Rows[0][7].ToString();
        user.Email = dt.Rows[0][8].ToString();
        return user;
    }

    public int getIdByUsername(string username)
    {
        User user = getUserByUsername(username);
        int userId = user.UserId;
        return userId;
    }

    public bool insertUser(User user)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[8];
        para[0] = new SqlParameter("@Username", SqlDbType.NVarChar);
        para[0].Value = user.UserName;
        para[1] = new SqlParameter("@Password", SqlDbType.NVarChar);
        para[1].Value = user.Password;
        para[2] = new SqlParameter("@Fullname", SqlDbType.NVarChar);
        para[2].Value = user.FullName;
        para[3] = new SqlParameter("@Gender", SqlDbType.NVarChar);
        para[3].Value = user.Gender;
        para[4] = new SqlParameter("@Address", SqlDbType.NVarChar);
        para[4].Value = user.Address;
        para[5] = new SqlParameter("@Role", SqlDbType.NVarChar);
        para[5].Value = user.Role;
        para[6] = new SqlParameter("@Phone", SqlDbType.NVarChar);
        para[6].Value = user.Phone;
        para[7] = new SqlParameter("@Email", SqlDbType.NVarChar);
        para[7].Value = user.Email;
        if (db.execute("InsertUser", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool changePassword(string username, string password)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[2];
        para[0] = new SqlParameter("@Username", SqlDbType.NVarChar);
        para[0].Value=username;
        para[1]=new SqlParameter("@Password",SqlDbType.NVarChar);
        para[1].Value = password;
        if (db.execute("UpdatePasswordByUsername", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }
    public bool changeProfile(User user)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[6];
        para[0] = new SqlParameter("@Username", SqlDbType.NVarChar);
        para[0].Value = user.UserName;
        para[1] = new SqlParameter("@Fullname", SqlDbType.NVarChar);
        para[1].Value = user.FullName;
        para[2] = new SqlParameter("@Gender", SqlDbType.NVarChar);
        para[2].Value = user.Gender;
        para[3] = new SqlParameter("@Address", SqlDbType.NVarChar);
        para[3].Value = user.Address;
        para[4] = new SqlParameter("@Phone", SqlDbType.NVarChar);
        para[4].Value = user.Phone;
        para[5] = new SqlParameter("@Email", SqlDbType.NVarChar);
        para[5].Value = user.Email;
        if (db.execute("UpdateProfileByUsername", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool deleteUserById(int userId)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@UserId", SqlDbType.Int);
        para[0].Value = userId;
        if (db.execute("DeleteUserById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }   
}
